<?php

namespace App\Model\Account;

use App\Core\BaseModel;
use App\Model\Member\MemberModel;
use EasySwoole\Component\Di;
use EasySwoole\Redis\Redis;

/**
 * CREATE TABLE `ss_cashout` (
 * `id` int(11) NOT NULL AUTO_INCREMENT,
 * `user_id` int(11) NOT NULL DEFAULT '0' COMMENT '用户id',
 * `amout` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '提现金额',
 * `service_fee` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '手续费',
 * `account_type` char(10) NOT NULL DEFAULT '' COMMENT '提现账户类型 FUNDS:本金账户;PROFIT:收益账户',
 * `cash_state` char(20) NOT NULL COMMENT '提现状态 INPROGRESS:进行中,FINISHED:已完成,CANCELLED:已取消',
 * `payment_type` char(20) NOT NULL COMMENT '提现方式: USDT:usdt,WECHAT:微信,ALIPAY:支付宝,PUB_BANK:对公,PRI_BANK:对私',
 * `payment_content` varchar(255) NOT NULL DEFAULT '' COMMENT '支付凭证',
 * `payment_date` date DEFAULT NULL COMMENT '支付时间',
 * `remark` varchar(255) CHARACTER SET utf8mb4 NOT NULL DEFAULT '' COMMENT '备注',
 * `created_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT '创建时间',
 * `created_by` varchar(32) CHARACTER SET utf8mb4 NOT NULL DEFAULT '' COMMENT '创建人员账号',
 * `updated_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' ON UPDATE CURRENT_TIMESTAMP COMMENT '最后更新时间',
 * `updated_by` varchar(32) CHARACTER SET utf8mb4 NOT NULL DEFAULT '' COMMENT '最后更新人员账号',
 * `is_delete` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否删除[0:未删除，1：已删除]',
 * `deleted_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT '删除时间',
 * PRIMARY KEY (`id`) USING BTREE
 * ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户提现表';
 */
/**
 * Class CashoutModel
 * @property int id
 * @property int user_id
 * @property float amout
 * @property float service_fee
 * @property string account_type
 * @property string cash_state
 * @property string payment_type
 * @property string payment_content
 * @property string payment_date
 * @property string remark
 * @property string created_at
 *
 * */
class CashoutModel extends BaseModel
{

    protected $tableName = 'ss_cashout';
    protected static $logTableName = 'ss_cashout_log';

    protected $casts = [
        'id'              => 'integer',
        'user_id'         => 'integer',
        'amout'           => 'float',
        'service_fee'     => 'float',
        'account_type'    => 'string',
        'cash_state'      => 'string',
        'payment_type'    => 'string',
        'payment_content' => 'string',
        'payment_date'    => 'string',
        'remark'          => 'string',
        'created_at'      => 'string',
    ];

    //提现账户类型 FUNDS:本金账户;PROFIT:收益账户
    const ACCOUNT_TYPE_FUNDS   = 'FUNDS';//本金账户
    const ACCOUNT_TYPE_PROFIT  = 'PROFIT';//收益账户

    //充值状态 INPROGRESS:进行中,FINISHED:已完成,CANCELLED:已取消
    const INPROGRESS = 'INPROGRESS';//进行中
    const FINISHED   = 'FINISHED';//已完成
    const CANCELLED  = 'CANCELLED';//已取消

    //充值方式: USDT:usdt,WECHAT:微信,ALIPAY:支付宝,PUB_BANK:对公,PRI_BANK:对私
    const PAYMENT_USDT     = 'USDT';//usdt
    const PAYMENT_BINANCE     = 'BINANCE';//币安
    const PAYMENT_TRX     = 'TRX';//波场
    const PAYMENT_WECHAT   = 'WECHAT';//微信
    const PAYMENT_ALIPAY   = 'ALIPAY';//支付宝
    const PAYMENT_PUB_BANK = 'PUB_BANK';//对公
    const PAYMENT_PRI_BANK = 'PRI_BANK';//对私

    //支付状态:UNPAID:未支付, PAID:已支付
    const UNPAID = 'UNPAID';//未支付
    const PAID   = 'PAID';//已支付

    //收益账户两周提现一次，每次提现100起，只能是整数倍
    const PROFIT_PERIOD = 0; //14天
    const PROFIT_MIN = 100;
    const ALLOW_DATE = [10, 20, 30];//可提现日期
    const SERVICE_FEE = 2;

    public static function getAccountTypeDict($key = null)
    {
        return self::getMap([
//            self::ACCOUNT_TYPE_FUNDS   => '本金账户',
            self::ACCOUNT_TYPE_PROFIT  => '收益账户',
        ], $key);
    }
    public static function getCashStateDict($key = null)
    {
        return self::getMap([
            self::INPROGRESS => '进行中',
            self::FINISHED   => '已完成',
            self::CANCELLED  => '已取消',
        ], $key);
    }

    public static function getPaymentTypeDict($key = null)
    {
        return self::getMap([
//            self::PAYMENT_USDT     => 'USDT',
            self::PAYMENT_BINANCE     => '币安',
            self::PAYMENT_TRX     => '波场',
//            self::PAYMENT_WECHAT   => '微信',
//            self::PAYMENT_ALIPAY   => '支付宝',
//            self::PAYMENT_PUB_BANK => '对公账户',
//            self::PAYMENT_PRI_BANK => '对私账户',
        ], $key);
    }

    public function member()
    {
        return $this->hasOne(MemberModel::class, function ($query) {
            $query->where('is_delete', 0);
            return $query;
        }, 'user_id', 'id');
    }

    protected static function onAfterUpdate($model, $res)
    {
        self::delCache('cashoutCache:');
    }

    protected static function onAfterInsert($model, $res)
    {
        self::delCache('cashoutCache:');
    }
}
